package com.gdth.bank.activity;

import java.util.ArrayList;
import java.util.List;

import com.bumptech.glide.Glide;
import com.gdth.bank.R;
import com.gdth.bank.adapter.PhotoPageAdapter;
import com.gdth.bank.presenter.CjkhFjxxPresenter;
import com.gdth.bank.utils.FileUtils;
import com.gdth.bank.view.CjkhFjxxView;
import com.gdth.bank.widget.image.Bimp;
import com.gdth.core.annotation.view.ViewInject;
import com.gdth.core.widget.BaseActivity;
import com.gdth.database.bean.TAppFjxxb;
import com.gdth.utils.ToolUtil;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class CjkhPhotoActivity extends BaseActivity implements CjkhFjxxView{
    
	@ViewInject(id=R.id.viewpager) ViewPager pager;
	
	@ViewInject(id=R.id.photo_relativeLayout) RelativeLayout photo_relativeLayout;
	
	@ViewInject(id=R.id.photo_bt_exit, click="onFinishClick") Button photo_bt_exit;
	@ViewInject(id=R.id.photo_bt_del, click="onDeleteClick") Button photo_bt_del;
	@ViewInject(id=R.id.photo_bt_enter, click="onEnterClick") Button photo_bt_enter;
	
	private ArrayList<View> listViews = new ArrayList<View>();
	private PhotoPageAdapter adapter;
	private int count;

	public List<Bitmap> bmp = new ArrayList<Bitmap>();
	public List<TAppFjxxb> fjxxs = new ArrayList<TAppFjxxb>();
	
	CjkhFjxxPresenter presenter;
	
	int current = 1;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_photo);

		initView();
	}
	
	private void initView() {
		photo_bt_del.setVisibility(View.GONE);
		Intent intent = getIntent();
		current = intent.getIntExtra("ID", 0);
		String jcuuid = intent.getStringExtra("jcuuid");
		
		presenter = new CjkhFjxxPresenter(this);
		presenter.getFjxxByUuid(jcuuid);
	}
	
	public void onEnterClick(View v) {
//			FileUtils.delFile(del.get(i) + ".JPEG");
		finish();
	}
	
	public void onDeleteClick(View v) {
		if (listViews.size() == 1) {
			Bimp.getInstance().reset();
			FileUtils.deleteDir();
			finish();
		} else {
			pager.removeAllViews();
			listViews.remove(count);
			adapter.setListViews(listViews);
			adapter.notifyDataSetChanged();
		}
	}
	
	public void onFinishClick(View v){
		finish();
	}

	private OnPageChangeListener pageChangeListener = new OnPageChangeListener(){
		public void onPageSelected(int arg0) {
			count = arg0;
		}

		public void onPageScrolled(int arg0, float arg1, int arg2){

		}

		public void onPageScrollStateChanged(int arg0){

		}
	};

	@Override
	public void showProgressDialog(String msg) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void hideProgressDialog() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void showMessage(String msg) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void saveSuccess(Long id) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void getAllFjxx(List<TAppFjxxb> arg0) {
		// TODO Auto-generated method stub
		listViews.clear();
		for (TAppFjxxb mTAppFjxxb : arg0) {
			fjxxs.add(mTAppFjxxb);
			
			ImageView img = new ImageView(this);
			img.setBackgroundColor(0xff000000);
			
			String fullPath = ToolUtil.getFileFullPath(ToolUtil.getFilePath(mTAppFjxxb.getUuid() + "." + mTAppFjxxb.getWjlx()));
		
            Glide.with(this).load(fullPath).into(img);
			img.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
			listViews.add(img);
		}
		pager.addOnPageChangeListener(pageChangeListener);
		
		adapter = new PhotoPageAdapter(listViews);//  构造adapter
		pager.setAdapter(adapter);//  设置适配器
//		adapter.notifyDataSetChanged();
		pager.setCurrentItem(current);
	}

	@Override
	public void saveSuccess(int size) {
		// TODO Auto-generated method stub
		
	}

	
}
